package com.example.shellshopping;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.fragment.app.Fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shellshopping.login.PersonCenterMainActivity;
import com.example.shellshopping.login.UserBean;
import com.example.shellshopping.utils.SaveUserUtils;

import java.util.ArrayList;
import java.util.List;

//表示页面，对应布局，查找布局当中的控件
//1、声明控件的成员变量  2、查找控件对应的xml的id  3、设置用户互动监听
    public class MainActivity extends AppCompatActivity {
    RadioGroup rg;
    Button fpagebth,typebtn,mebtn,loginbtn,regbtn,cancelbtn;
    TextView logintv,me_btn3;
    DrawerLayout drawerLayout;

   // private FragmentMananger fm;
    private FragmentManager fm;

    //将当前的activity包含的fragment放到一个集合里，便于管理
    List<Fragment>fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListen();
        initFragment();
    }

    //  获取焦点的执行方法
    @Override
    protected  void onResume(){
        super.onResume();
        setWelcomeText();  //设置改变彩色部分内容
    }

    private void setWelcomeText() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        if(userInfo == null){
            logintv.setText("未登录");
        }else{
            String nickname = userInfo.getNickname();
            String msg = nickname+"用户，\n欢迎您";
            logintv.setText(msg);
        }
    }

    //初始化fragment管理集合
    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new FPageFragment());
        fragmentList.add(new TypeFragment());
        fragmentList.add(new MeFragment());
        //将碎片添加到activity当中
        //1、获取碎片管理对象
        fm = getSupportFragmentManager();
        //2、获取碎片事物对象
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            transaction.add(R.id.main_layout,fragmentList.get(i));
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(0));
        transaction.commit();
    }
    //切换Fragment
    private void changeFragment(int pos){
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(pos));
        transaction.commit();
    }
    //设置监听事件
    private void setListen(){
        fpagebth.setOnClickListener(onClickListener);
        typebtn.setOnClickListener(onClickListener);
        mebtn.setOnClickListener(onClickListener);
        loginbtn.setOnClickListener(onClickListener);
        regbtn.setOnClickListener(onClickListener);
        cancelbtn.setOnClickListener(onClickListener);
//        me_btn3.setOnClickListener(onClickListener);
        rg.setOnCheckedChangeListener(listener);
    }
    //创建监听对象
    View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v){
            int id = v.getId();  //获取被点击的id
            switch (id) {
                case R.id.drawer_btn:
                    rg.check(R.id.main_rb1);
                    break;
                case R.id.drawer_btn_type:
                    rg.check(R.id.main_rb2);
                    break;
                case R.id.drawer_btn_me:
                    rg.check(R.id.main_rb3);
                    break;
                case R.id.drawer_login:
                    //判断是否登陆了，如果登陆了就跳转到个人中心页面，否则跳转到登陆界面
                    boolean login = isLogin();
                    Intent it1 = new Intent(MainActivity.this,LoginRegActivity.class);
                    if (!login) {
                        //跳转登录界面
                        it1.putExtra("flag",0);
                        startActivity(it1);
                    }
                    break;
                case R.id.drawer_btn_reg:
                    //跳转注册界面
                    it1 = new Intent(MainActivity.this,LoginRegActivity.class);
                    it1.putExtra("flag",1);
                    startActivity(it1);
                    break;

                case R.id.drawer_btn_cancel:
                    //注销
                    exitLogin();
                    break;
            }
            drawerLayout.closeDrawers();  //关闭抽屉
        }
    };
    //  退出登录，注销
    private void exitLogin() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        if (userInfo == null) {
            Toast.makeText(this, "您还未登录账号", Toast.LENGTH_SHORT).show();
        }else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息").setMessage("您是否退出登录？")
                    .setPositiveButton("取消",null)
                    .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //删除文件中的内容
                            SaveUserUtils.deleteUserInfo(MainActivity.this);
                            logintv.setText("未登录");
                            Toast.makeText(MainActivity.this, "注销成功", Toast.LENGTH_SHORT).show();
                            //MeFragment mefrag = (MeFragment)fragmentList.get(2);
                            //mefrag.setHeadview;
                        }
                    });
            builder.show();
        }
    }

    //判断是否登录，如果登录就跳转到个人中心页面
    private boolean isLogin() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        if(userInfo==null)
            return false;
        Intent intent = new Intent(this,PersonCenterMainActivity.class);
        startActivity(intent);
        return true;
    }

    RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group,int checkedId){
            switch (checkedId) {
                case R.id.main_rb1:
                    changeFragment(0);
                    break;
                case R.id.main_rb2:
                    changeFragment(1);
                    break;
                case R.id.main_rb3:
                    changeFragment(2);
                    break;
            }
        }
    };
    //查找控件
    private  void initView(){
        rg = findViewById(R.id.main_rg);
        fpagebth = findViewById(R.id.drawer_btn);
        typebtn = findViewById(R.id.drawer_btn_type);
        mebtn=findViewById(R.id.drawer_btn_me);
        loginbtn=findViewById(R.id.drawer_login);
        regbtn = findViewById(R.id.drawer_btn_reg);
        cancelbtn = findViewById(R.id.drawer_btn_cancel);
        logintv = findViewById(R.id.login_tv_name);
        drawerLayout = findViewById(R.id.main_drawer);
       // me_btn3 = findViewById(R.id.frag_me_btn3);
    }


}